﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;


public partial class Moral_Teacher_ScoreInput_WeekInput : ContainerBasePage
{
    protected static int EvalID
    {
        get
        {
            int _evalId;
            if (int.TryParse(HttpContext.Current.Request["id"], out _evalId))
                return _evalId;
            return -1;
        }
    }

    protected static int Attribute
    {
        get
        {
            int _attribute;
            if (int.TryParse(HttpContext.Current.Request["attribute"], out _attribute))
                return _attribute;
            return -1;
        }
    }

    protected static int EvalType
    {
        get
        {
            int _evalType;
            if (int.TryParse(HttpContext.Current.Request["evaltype"], out _evalType))
                return _evalType;
            return -1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    /// <summary>
    /// 初始化界面标签元素的显示行为
    /// </summary>
    protected void InitDisplay()
    {
        lbTitle.Text = "";
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("IsEffect", "1");
        //ht.Add("HeaderID", Common.GetUserInfo().UID);
        IList<ClsInfo> clsList = Container<IClsInfoLogic>.Instance.FindAll(ht);

        IList<int> idList = new List<int>();
        foreach (ClsInfo info in clsList)
        {
            idList.Add(info.RID);
        }

        ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
    //    DateTime termStart = (DateTime)infoList[0].StartDate;
        DateTime termStart = new DateTime();
        DateTime timeEnd = new DateTime();
        SchoolCommon.GetWeekInTerm(SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, SchoolMark, 1, out termStart, out timeEnd);

        IList<ClassEvalState> stateList =
           Container<IClassEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                               SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                               SchoolMark, EvalID, termStart, DateTime.Now);
        CreateTable(clsList, stateList,
            SchoolCommon.Sch_SchoolInfo.SchoolYear,
            SchoolCommon.Sch_SchoolInfo.SchoolTerm, termStart);

        lbTitle.Text = "本学期评价记录";
    }


    //private List<ClassEvalState> FilterStateList(int month, List<ClassEvalState> stateList)
    //{ 
    //    if(month > 0)
    //    {
    //        List<ClassEvalState>  newList = new List<ClassEvalState>();
    //        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
    //        int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
    //        int startWeek = 0;endWeek = 0;
    //        SchoolCommon.GetWeekInMonth(schoolYear,term,month,out startWeek, out endWeek);

    //        foreach (ClassEvalState item in stateList)
    //        {
    //            if(item.StartTime
    //        }
    //    }
        
    //}

    /// <summary>
    /// 构造结果表格
    /// </summary>
    private void CreateTable(IList<ClsInfo> classInfo, IList<ClassEvalState> stateInfo, int year, int term, DateTime termStart)
    {
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<ClassEvalState> sort = new ListSort<ClassEvalState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();


        //构造多少周
        int weeks = 0,month = 0,selYear =0;
        string s = txtYearMonth.Text;
        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        if (!string.IsNullOrEmpty(s))
        {
            string[] arr = s.Split('.');//以2009.6的格式组合的字符串，包括学年和学期信息
            if (arr.Length > 0)
            {
                 int.TryParse(arr[1], out month);
                 int.TryParse(arr[0], out selYear);
            }
            
        }

        int currWeek = 0;
        DateTime timeStart = new DateTime();//开始时间
        DateTime tiemEnd = new DateTime();
        SchoolCommon.GetWeekOfTerm(schoolYear,term,SchoolMark,DateTime.Now,out currWeek);
        if (month > 0)
        {
            int startWeek = 0, endWeek = 0;
            SchoolCommon.GetWeekInMonth(schoolYear, term, SchoolMark, selYear, month, out startWeek, out endWeek);
            for (int state = startWeek; state <= endWeek; state++)
            {
                if (state < 0)
                    continue;

                SchoolCommon.GetWeekInTerm(schoolYear, term, SchoolMark, (state+1), out timeStart, out tiemEnd);

                hcell = new TableHeaderCell();
                hcell.Text = String.Format("{0}周", state + 1);
                hcell.Style[HtmlTextWriterStyle.Width] = "75px";
                width += 75;
                hrow.Cells.Add(hcell);
                dateList.Add(timeStart);
                timeStart = timeStart.AddDays(7);
                //AddMonths(state));
              
                if (state == currWeek - 1)
                    break;
            }
        }
        else
        {
            if (SchoolCommon.GetWeekOfTerm(schoolYear, term, SchoolMark, DateTime.Now, out weeks))
            {
                for (int state = 0; state <= weeks; state++)
                {
                    SchoolCommon.GetWeekInTerm(schoolYear, term, SchoolMark, (state + 1), out timeStart, out tiemEnd);
                    hcell = new TableHeaderCell();
                    hcell.Text = String.Format("{0}周", state+1);
                    hcell.Style[HtmlTextWriterStyle.Width] = "75px";
                    width += 75;
                    hrow.Cells.Add(hcell);
                    dateList.Add(timeStart);
                    timeStart = timeStart.AddDays(7);
                    //AddMonths(state));

                    if (state == currWeek - 1)
                        break;
                }
            }
        }
        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
      
        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (ClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsName;
            row.Cells.Add(cell);

            int i = 0;
            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (ClassEvalState state in stateInfo)
                {
                    if (state.ClassID == info.RID && state.StartTime == dt)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        control.NavigateUrl = String.Format("../ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                                year, term,
                                                state.ClassID, state.StartTime.ToString("yyyy-MM-dd"), Attribute, EvalID);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    control.NavigateUrl = String.Format("../ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                            year, term,
                                            info.RID, dt.ToString("yyyy-MM-dd"), Attribute, EvalID);
                    cell.Controls.Add(control);
                }
                cell.Attributes.Add("onmouseout", "mout(" + i + ");");
                cell.Attributes.Add("onmouseover", "mover(" + i + ");");
                row.Cells.Add(cell);
                i++;
            }

            tbResult.Rows.Add(row);
        }
    }


    protected void SetMonth()
    {
        Response.Write("if(document.getElementById('selWeek') != null)");
        Response.Write("{");
        Response.Write("    var selWeek = document.getElementById('selWeek');");

        Response.Write("    var option = document.createElement('option');");
        Response.Write("option.selected = '';");
        Response.Write("    option.innerText = '本学期';");
        Response.Write("    option.value = '0.0';");
        Response.Write("    selWeek.appendChild(option);");

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;

        while (!(termStart.Month > DateTime.Now.Month && termStart > DateTime.Now))
        {
            string str = "";
            switch (termStart.Month)
            {
                case 1: str = "一月"; break;
                case 2: str = "二月"; break;
                case 3: str = "三月"; break;
                case 4: str = "四月"; break;
                case 5: str = "五月"; break;
                case 6: str = "六月"; break;
                case 7: str = "七月"; break;
                case 8: str = "八月"; break;
                case 9: str = "九月"; break;
                case 10: str = "十月"; break;
                case 11: str = "十一月"; break;
                case 12: str = "十二月"; break;
                default: break;
            }
 
            Response.Write("var option = document.createElement('option');");
            Response.Write("option.innerText = '" + str + "';");
            Response.Write("option.value = '" + termStart.ToString("yyyy.MM") + "';");
            if (termStart.Month == DateTime.Now.Month)
            {
                Response.Write("option.selected = 'selected ';");
            }
            else
            {
                Response.Write("option.selected = '';");
            }
            Response.Write("selWeek.appendChild(option);");
            Response.Write("selWeek.click();");
            termStart = termStart.AddMonths(1);
        }
        Response.Write("}");


    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        InitDisplay();
    }
}
